package com.yawn.zdproject.service;

import com.yawn.zdproject.dao.ProjectDao;
import com.yawn.zdproject.entity.Project;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class ProjectService {

    @Resource
    private ProjectDao projectDao;

    @CacheEvict(cacheNames = "pro", key = "'preferPro'")
    @Transactional(rollbackOn = Exception.class)
    public boolean setPreferPro(String code) {
        projectDao.resetPreferPro();
        projectDao.setPreferPro(code);
        return true;
    }

    @Cacheable(cacheNames = "pro", key = "'preferPro'")
    public Project getPreferPro() {
        return projectDao.getFirstByPrefer(true);
    }

    @Cacheable(cacheNames = "pro", key = "#code")
    public Project findOne(String code) {
        return projectDao.findOne(code);
    }

    @Cacheable(cacheNames = "pro", key = "'allPro'")
    public List<Project> findAll() {
        return projectDao.findAll();
    }

    @CacheEvict(cacheNames = "pro", allEntries = true)
    public void save(Project project) {
        projectDao.save(project);
    }

    @CacheEvict(cacheNames = "pro", allEntries = true)
    public void delete(Project pro) {
        projectDao.delete(pro);
    }
}
